package com.travel.server.manager.impl.command.data;

import com.travel.common.excel.ExcelData;
import com.travel.common.util.CollectionUtil;
import com.travel.common.util.DateUtil;
import com.travel.common.util.ExcelUtil;
import com.travel.server.manager.dto.request.EmptyRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.OrderDal;
import com.travel.server.manager.model.dto.data.WholeAllDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @Author: LiZhenhui
 * @Date: 2018/12/14 19:10
 * @Desc:
 */
@Component
@Slf4j
public class ExportWholeDataCommand extends AbstractCommand<EmptyRequest, EmptyResponse> {

    @Autowired
    private OrderDal orderDal;

    @Override
    protected void checkInput(EmptyRequest request) {

    }

    @Override
    public EmptyResponse handle(EmptyRequest request) {
        Date now = new Date();
        Date yesterday = DateUtil.getIndexDay(now, Calendar.DAY_OF_MONTH, -1);
        Date yesterdayStartTime = DateUtil.setDayStart(yesterday);
        Date yesterdayEndTime = DateUtil.setDayEnd(yesterday);
        WholeAllDto totalDto = CollectionUtil.getFirst(orderDal.selectAllData(null, null));
        WholeAllDto yesterdayDto = CollectionUtil.getFirst(orderDal.selectAllData(yesterdayStartTime, yesterdayEndTime));

        List<String> title = Arrays.asList("总访客数", "昨日全天访客数", "总浏览量", "昨日全天浏览量", "总付款订单数", "昨日全天付款订单数", "总付款人数", "昨天全天付款人数");
        List<List<Object>> rows = new ArrayList<>();
        List<Object> objects = new ArrayList<>();
        objects.add("0");
        objects.add("0");
        objects.add("0");
        objects.add("0");
        objects.add(totalDto.getPayNum());
        objects.add(yesterdayDto.getPayNum());
        objects.add(totalDto.getPayNum());
        objects.add(yesterdayDto.getPayNum());
        rows.add(objects);
        ExcelData excelData = new ExcelData(title, rows, "实时概况");
        ExcelUtil.exportExcel(getHttpServletResponse(), "实时概况", excelData);
        return new EmptyResponse();
    }
}
